//
//  ZDLoginView.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/10/26.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

//MARK: - 代理协议
protocol ZDVisitorViewDelegate : NSObjectProtocol {

    //默认是required  必须实现
    func VisitorViewRegisterButtonClick()
    func VisitorViewLoginButtonClick()
}


class ZDVisitorView: UIView {
    
    
    //定义代理属性   可选类型 表示代理属性可能有值 也可能没有值
    weak var visitorViewDelegate: ZDVisitorViewDelegate?

    //MARK: - 重写构造函数 添加子控件
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        
        //添加子控件
        prepareForUI()
        
    }

    //MARK: - 从nib文件加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 添加子控件
    private func prepareForUI() {
        
        //设置当前view的背景颜色 ———— 目的是为了与遮盖颜色混合成一样
        backgroundColor = UIColor(white:237 / 255.0, alpha:1.0)
        
        
        //添加子控件
        addSubview(iconView)
        //添加遮盖  ————  遮盖添加到iconView上面 homeView下面
        addSubview(coverView)
        addSubview(homeView)
        addSubview(label)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //代码设置子控件的autolayout
        //关闭Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        label.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //iconView添加约束  添加到共同的父控件中
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))   //水平居中
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))   //垂直方向居中有间距
        
        //coverView添加约束  添加到共同的父控件中
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        //homeView添加约束  添加到共同的父控件中
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //label添加约束  添加到共同的父控件中
        addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 240))
        
        //registerButton添加约束  添加到共同的父控件中
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: label, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: label, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //loginButton添加约束  添加到共同的父控件中
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: label, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: label, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
    }
    
    
    //MARK: - 修改访客视图  供外界调用
    func setupVisitorView(imageName: String, title: String) {
        
        //隐藏homeView
        homeView.hidden = true

        //移动遮盖到最底层
        sendSubviewToBack(coverView)
        
        //修改iconView
        iconView.image = UIImage(named: imageName)
        
        //修改label
        label.text = title
    }
    
    
    //MARK: - iconView旋转方法 供外界调用
    func startRotation() {
    
        //创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        //重复次数 无穷大
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 20
        
        //设置动画在停止后不移除  ———— 解决切换到其他控制器后再返回时动画停止的问题(原因是因为动画被移除了，可再touchesBegin...方法中打印验证)
        animation.removedOnCompletion = false
        
        //添加到图层中
        iconView.layer.addAnimation(animation, forKey: "rotationAnimation")
    }
    
    
    //验证动画移除
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        print(iconView.layer.animationKeys())
    }
    
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    
    //MARK: - 按钮点击事件  不能定义成pravite方法
    func registerButtonClick() {
    
        //需要modal出控制器  由代理来完成
        visitorViewDelegate?.VisitorViewRegisterButtonClick()
    }
    
    func loginButtonClick() {
        
        visitorViewDelegate?.VisitorViewLoginButtonClick()
    }
    

    //MARK: - 懒加载
    //MARK: - iconView
    private lazy var iconView: UIImageView = {
    
        let iconView = UIImageView()
        
        iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        //设置根据内容大小调整size
        iconView.sizeToFit()
        
        return iconView
    }()
    
    
    //MARK: - homeView
    private lazy var homeView: UIImageView = {
        
        let homeView = UIImageView()
        
        homeView.image = UIImage(named: "visitordiscover_feed_image_house")
        
        //设置根据内容大小调整size
        homeView.sizeToFit()
        
        return homeView
    }()

    
    //MARK: - label
    private lazy var label: UILabel = {
    
        let label = UILabel()
        
        label.text = "关注一些人，看看有什么惊喜"
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0   //换行
        
        //设置根据内容大小调整size
        label.sizeToFit()
        
        return label
    }()
    
    
    //MARK: - registerButton
    private lazy var registerButton: UIButton = {
    
        let registerButton = UIButton()
        
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置根据内容大小调整size
        registerButton.sizeToFit()
        
        //添加点击事件
        registerButton.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerButton
    }()
    
    
    //MARK: - loginButton
    private lazy var loginButton: UIButton = {
        
        let loginButton = UIButton()
        
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        loginButton.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //设置根据内容大小调整size
        loginButton.sizeToFit()
        
        //添加点击事件
        loginButton.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
    }()
    
    
    //MARK: - 添加遮盖
    private lazy var coverView: UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon"))
}
